﻿//**************************
//开发人员：Morning Sun QQ:576830428  微信:MorningSun0125
//业务范围：网站开发、小程序开发、web应用开发,桌面程序开发,工控类软件开发等
//开发日期：2016年1月10日
//版本号：Version 3.0 CopyRight 2011-2029
//**************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileServer
{
    public partial class FormClient : Form
    {
        #region 变量
        /// <summary>
        /// 拖动位置
        /// </summary>
        private Point MousePoint;
        /// <summary>
        /// 是否为鼠标左键
        /// </summary>
        private bool LeftFlag;

        /// <summary>
        /// 客户端信息
        /// </summary>
        DalTbClient dalclient = new DalTbClient();
        #endregion


        #region 构造函数
        public FormClient()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            CenterToScreen();

            #region 工具栏
            WinIcon.Parent = WinTitlePic;
            labWinTitle.Parent = WinTitlePic;
            labWinMin.Parent = WinTitlePic;
            labWinClose.Parent = WinTitlePic;
            labWinMin.BackgroundImage = global::FileServer.Properties.Resources.winMin;
            labWinClose.BackgroundImage = global::FileServer.Properties.Resources.winClose;
            #endregion

            #region 工作区
            PBox.Parent = WorkPic;
            #endregion

            //绑定列表
            BindGrid();
        }
        #endregion


        #region   最小化窗体
        /// <summary>
        /// 最小化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion


        #region 关闭窗体
        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion


        #region  最小化按钮效果
        /// <summary>
        /// 鼠标悬浮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinMin_MouseHover(object sender, EventArgs e)
        {
            labWinMin.BackgroundImage = global::FileServer.Properties.Resources.winMinPressed;
        }


        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinMin_MouseLeave(object sender, EventArgs e)
        {
            labWinMin.BackgroundImage = global::FileServer.Properties.Resources.winMin;
        }
        #endregion


        #region  关闭按钮效果
        /// <summary>
        /// 关闭按钮悬浮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinClose_MouseHover(object sender, EventArgs e)
        {
            labWinClose.BackgroundImage = global::FileServer.Properties.Resources.winClosePressed;
        }


        /// <summary>
        /// 关闭按钮鼠标经过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labWinClose_MouseLeave(object sender, EventArgs e)
        {
            labWinClose.BackgroundImage = global::FileServer.Properties.Resources.winClose;
        }
        #endregion


        #region 窗体拖动
        private void WinTitlePic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MousePoint = new Point(-e.X, -e.Y); //得到变量的值
                LeftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        private void WinTitlePic_MouseMove(object sender, MouseEventArgs e)
        {
            if (LeftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(MousePoint.X, MousePoint.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void WinTitlePic_MouseUp(object sender, MouseEventArgs e)
        {
            if (LeftFlag)
            {
                LeftFlag = false;//释放鼠标后标注为false;
            }
        }
        #endregion


        #region  按钮效果
        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = global::FileServer.Properties.Resources.btn;
        }


        /// <summary>
        /// 鼠标悬浮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = global::FileServer.Properties.Resources.btn_over;
        }
        #endregion



        #region 绑定列表
        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindGrid()
        {
            DataSet ds = dalclient.GetListBySql("Select ID,ClientIP,ClientDir from tbClient ");
           ClientGridView.DataSource = ds.Tables[0];
        }
        #endregion


        #region 新增
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            TbxIP.Text = "";
            TbxDir.Text = "";
            LabID.Text = "";
        }
        #endregion


        #region 编辑
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (ClientGridView.SelectedRows.Count > 0)
            {
                if (ClientGridView.SelectedRows.Count== 1)
                {
                    LabID.Text = ClientGridView.SelectedRows[0].Cells[0].Value.ToString();
                    TbxIP.Text = ClientGridView.SelectedRows[0].Cells[1].Value.ToString();
                    TbxDir.Text = ClientGridView.SelectedRows[0].Cells[2].Value.ToString();
                }
                else
                {
                    MessageBox.Show("请选择一条记录编辑!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选择要编辑的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion 


        #region 保存
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string Result=CheckConfig();
            if (Result.Trim() == "")
            {
                TbClient client = new TbClient();
                client.ClientDir = TbxDir.Text.Trim();
                client.ClientIP = TbxIP.Text.Trim();
                int result = 0;
                if (LabID.Text.Trim() != "0")
                {
                    client.ID = int.Parse(LabID.Text.Trim());
                    result = dalclient.UpDateByModel(client);
                }
                else
                {
                    result = dalclient.AddByModel(client);
                }
                if (result > 0)
                {
                    TbxIP.Text = "";
                    TbxDir.Text = "";
                    LabID.Text = "";
                    BindGrid();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show(Result, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion


        #region 关闭
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion


        #region 检测配置
        /// <summary>
        /// 检测配置
        /// </summary>
        /// <returns></returns>
        private string CheckConfig()
        {
            string IP = TbxIP.Text.Trim();
            string Dir = TbxDir.Text.Trim();
            string Result = "";
            if (FormValidate.IsEmpt(IP))
            {
                Result = "请填写服务器IP!";
            }
            else if (!FormValidate.IsIP(IP))
            {
                Result = "服务器IP格式错误!";
            }
            else if (FormValidate.IsEmpt(Dir))
            {
                Result = "请填写同步目录!";
            }
            else if (!FormValidate.IsDirPath(Dir))
            {
                Result = "同步目录格式不正确!";
            }
            return Result;
        }
        #endregion
    }
}
